<?php
/**
 * 订单系统  属于半独立系统，权限方面不考虑
 * 2013-2-27
 * @author wolf Email: 116311316@qq.com
 *
 */
class OrderController extends NodeController
{
    protected $shxx = array(); //获取收货信息
    protected $user = array();
    //以下三个参数对应的是积分记录 而不是订单
    private $chargeType = array("充值" => 0, "消费" => 1, "赠送" => 2);
    private $payment = array("收入" => 0, "支出" => 1);
    private $status = array("全部" => "0", "等待审核" => 1, "成功" => 8, "关闭" => - 1);
    /**
     * 产品类型 由系统检查而不是用户提交
     * @var unknown_type
     */
    private $_goodstype = array("coupons", "money");
    public function setAudit ()
    {
        if (empty($_POST)) {
            return;
        }
        $id = self::getOrderService()->setAudit($_POST);
        if ($id > 0) {
            $this->sendNotice("success!");
        } else {
            $this->sendNotice("failed!");
        }
    }
    /**
     * 订单查询
     */
    public function search ()
    {
        $rs = OrderModel::instance()->getOrderByWhere(
        array($_POST['type'] => $_POST['value']));
        $status = array_flip($this->status);
        $status = array_flip($this->status);
        foreach ($rs as $k => $v) {
            $rs[$k]['status'] = $status[$v['status']];
        }
        $this->view()->assign('totalnum', 1);
        $this->view()->assign('news', $rs);
        $this->view()->display('file:order/olist.tpl');
    }
    /**
     * 预览订单
     * Enter description here ...
     */
    public function create ()
    {
        //获取用户信息
        if (! isset($_SESSION['uid'])) {
            $this->redirect("请先登录", "/anonymous/login/");
            exit();
        }
        $user = unserialize($_SESSION['uid']);
        $userinfo = MemberModel::instance()->getOneMember(
        array('uid' => $user['uid']));
        //获取提交的订单信息 过滤多余字段
        $remark = trim($_POST['remark']);
        //获取提交的信息 只要产品id和产品数量 
        $data = array();
        foreach ($_POST['id'] as $k => $v) {
            if (empty($_POST['num'][$k])) {
                continue;
            }
            //非法字符检测
            $data[$k]['id'] = trim($v);
            $data[$k]['num'] = trim($_POST['num'][$k]);
        }
        if (empty($data)) {
            echo "购物车是空的";
            return;
        }
        //判断产品类型
        //判断用户积分或者金额是否足够
        //获取sno号
        $rs = OrderModel::instance()->getSno();
        $time = time();
        $sno = $rs['value'] . date('YmdHis', $time);
        $ginfo = $this->getGoodsInfo($data, $sno);
        if ($userinfo['coupons'] < $ginfo['couponstotal']) {
            //  echo "积分不够" . $userinfo['coupons'];
            //  echo $ginfo['couponstotal'];
            $this->redirect("积分不够", "/a/c/1031.html");
            return;
        }
        //订单列表  生成唯一sno号 判断库存是否有库存
        $this->addOrder($sno, $ginfo['name'], $user['uid'], 
        $userinfo['real_name'], $userinfo['real_name'], $userinfo['area'], 
        $userinfo['mobile_phone'], $ginfo['num'], $ginfo['couponstotal'], 
        $ginfo['moneytotal'], $remark, $this->status['等待审核']);
        //订单明细
        $this->addGoods($ginfo['goodslist']);
        //扣除积分或者金额      
        $this->saveUserCoupons($user['uid'], $ginfo['couponstotal'], 
        $ginfo['name'], $this->payment["支出"], $this->chargeType["消费"], $sno, 
        $this->status['等待审核']);
        $this->redirect("提交成功", "/order/trade/");
    }
    /**
     * 订单列表
     * Enter description here ...
     */
    public function listing ()
    {
        // 默认设置为15条
        if ($_GET['num'] > 0) {
            $num = trim($_GET['num']);
        } else {
            $newNum = require 'config/base.php';
            $num = 40;
        }
        if (! empty($_REQUEST['value'])) {
            $where = array($_REQUEST['type'] => trim($_REQUEST['value']));
        } else {
            $where = null;
        }
        // $totalNum = 2000;
        $totalNum = OrderModel::instance()->getCommentNum();
        $page = $this->page($totalNum, $num);
        $this->view()->assign('num', $page);
        $rs = OrderModel::instance()->commentPage($where, $page['start'], 
        $page['num']);
        $status = array_flip($this->status);
        foreach ($rs as $k => $v) {
            $rs[$k]['status'] = $status[$v['status']];
        }
        $this->view()->assign('totalnum', $totalNum);
        $this->view()->assign('news', $rs);
        $this->view()->assign('status', $this->status);
        $this->view()->assign("cachetype", 1);
        $this->view()->display('file:order/olist.tpl');
    }
    /**
     * 导出明细
     * */
    public function export ()
    {
        $this->setCsvHeader();
        /*按照时间到处*/
        if (isset($_POST['uid']) && $_POST['uid'] > 0) {
            $where = array('a.status' => 8, 'a.uid' => $_POST['uid']);
            $userinfo = MemberModel::instance()->getOneMember(
            array('uid' => $_POST['uid']));
        } else {
            $where = array('a.status' => 8);
            $userinfo['real_name'] = "全部";
        }
        $starttime = strtotime(str_replace(".", "-", $_POST['starttime']));
        $endtime = strtotime(
        str_replace(".", "-", $_POST['endtime'] . " 23:59:59"));
        $shijianduan = $_POST['starttime'] . "-" . $_POST['endtime'] .
         " 23:59:59";
        //统计期初
        $beforeIncome = MemberModel::instance()->getIncomeByBeforeTime(
        $starttime, $where);
        $beforeSpend = MemberModel::instance()->getSpendByBeforeTime($starttime, 
        $where);
        $income = MemberModel::instance()->getIncomeByTime($starttime, $endtime, 
        $where);
        $spend = MemberModel::instance()->getSpendByTime($starttime, $endtime, 
        $where);
        $detail = MemberModel::instance()->getCouponsHistoryAll($starttime, 
        $endtime, $where);
        $downloadtime = date("Y-m-d H:i:s", time());
        $baseinfo = "姓名,%s\r\n时间段,%s\r\n期初,%s\r\n订单数,%s\r\n积分收入,%s\r\n积分支出,%s\r\n积分余额,%s\r\n下载时间,%s";
        $couponsBalance = $beforeIncome['income'] + $beforeSpend['spend'] +
         $income['income'] + $spend['spend'];
        echo sprintf($baseinfo, $userinfo['real_name'], $shijianduan, 
        $beforeIncome['income'] + $beforeSpend['spend'], count($detail), 
        $income['income'], $spend['spend'], $couponsBalance, $downloadtime);
        echo "\r\n";
        echo $title = "流水号,充值时间,渠道,名称,收款人,军区,城市,收入,支出,收支,状态";
        echo "\r\n";
        $foramt = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s";
        $payment = array_flip($this->payment);
        $chargeType = array_flip($this->chargeType);
        foreach ($detail as $v) {
            $v['status'] = $v['status'] == 8 ? "成功" : "失败";
            if ($v['payment'] == 1) {
                echo sprintf($foramt, $v['id'], date("Y-m-d H:i:s", $v['date']), 
                $chargeType[$v['chargetypes']], $v['remark'], $v['real_name'], 
                $v['country'], $v['area'], "", $v['coupons'], 
                $payment[$v['payment']], $v['status']);
            } else {
                echo sprintf($foramt, $v['id'], date("Y-m-d H:i:s", $v['date']), 
                $chargeType[$v['chargetypes']], $v['remark'], $v['real_name'], 
                $v['country'], $v['area'], $v['coupons'], "", 
                $payment[$v['payment']], $v['status']);
            }
            echo "\r\n";
        }
        exit();
    }
    /**
     * 修改订单状态 和积分状态
     * Enter description here ...
     */
    public function setStatus ()
    {
        //设置订单状态  用户只能关闭 管理员审核  
        $user = unserialize($_SESSION['uid']);
        //获取登录用户信息
        $userinfo = MemberModel::instance()->getOneMember(
        array('uid' => $user['uid']));
        //检查当前状态
        $order = OrderModel::instance()->getOrderByWhere(
        array('orderno' => $_POST['orderno']));
        $order = $order[0];
        $orderUserinfo = MemberModel::instance()->getOneMember(
        array('uid' => $order['uid']));
        if (empty($order))
            return;
             //订单为成功或者失败后就不允许修改
        if ($order['status'] == $this->status['成功'] ||
         $order['status'] == $this->status['失败']) {
            return;
        }
        //检查权限 不是自己的就不能关闭 把自己订单设为成功
        /*
        if ($order['uid'] != $user['uid'] && $user['manager'] != 1) {
            return;
        }
        */
        //如果status为关闭 就归还积分
        OrderModel::instance()->saveStatus(
        array('status' => $_POST['status'], 'pay_time' => time(), 
        'action' => $user['real_name']), array('orderno' => $_POST['orderno']));
        MemberModel::instance()->setCouponsStatus(
        array('status' => $_POST['status']), 
        array('orderno' => $_POST['orderno']));
        if ($_POST['status'] == - 1) {
            $coupons = $order['coupons'] + $orderUserinfo['coupons'];
            MemberModel::instance()->setMemberByWhere(
            array('coupons' => $coupons), array('uid' => $orderUserinfo['uid']));
        }
        $this->sendNotice("成功");
    }
    /**
     * 添加订单
     * Enter description here ...
     */
    public function add ()
    {
        $data = json_decode($_POST['goodsinfo']);
        $info = array();
        $i = 0;
        $totalcount = 0;
        foreach ((array) $data as $k => $v) {
            $v = (array) $v;
            $goodsinfo = NewsModel::instance()->getNewsWhere(array('id' => $k));
            $info[$i]['id'] = $k;
            $info[$i]['image'] = $goodsinfo['thumb'];
            $info[$i]['count'] = $v['count'];
            $info[$i]['name'] = $v['name'];
            $info[$i]['price'] = $v['price'];
            $i ++;
            $totalcount += $v['count'] * $v['price'];
        }
        $this->view()->assign("totalcount", $totalcount);
        $this->view()->assign("news", $info);
        $this->view()->display("mysql:orderinfo.tpl");
         //显示在线支付
    }
    /*获取订单详情*/
    public function i ()
    {
        if (! empty($_GET['sno'])) {
            $goods = OrderModel::instance()->getGoodsBySNO(trim($_GET['sno']));
            /*增加算法,代理价优先*/
            $order = OrderModel::instance()->getOrderByWhere(
            array('orderno' => trim($_GET['sno'])));
            $order = $order[0];
            $this->view()->assign('order', $order);
            $this->view()->assign('goods', $goods);
            $this->view()->assign('p', $_GET['p']);
            $this->view()->assign('cachetype', 1);
            if ($_GET['type'] == 'manager') {
                /*增加提示信息 是否多人同时操作*/
                $wl = NewsModel::instance()->getNewsByWhere(
                array('cid' => 1079));
                $this->view()->assign('wl', $wl);
                $this->view()->clearCache('file:order/goods.tpl');
                $this->view()->display('file:order/goods.tpl'); /*产品详情页*/
            }
        }
    }
    //充值明细
    public function charge ()
    {
        // 默认设置为15条
        if ($_GET['num'] > 0) {
            $num = trim($_GET['num']);
        } else {
            $newNum = require 'config/base.php';
            $num = 40;
        }
        $totalNum = OrderModel::instance()->countOrderHisotry();
        $page = $this->page($totalNum, $num);
        $this->view()->assign('num', $page);
        $this->view()->assign('totalnum', $totalNum);
        if (isset($_GET['uid']) && $_GET['uid'] != "") {
            $detail = MemberModel::instance()->getCouponsHistory(
            array('uid' => trim($_GET['uid'])));
            $detail = MemberModel::instance()->getCouponsHistoryPage(
            $page['start'], $page['num'], array('a.uid' => $_GET['uid']));
            $user = MemberModel::instance()->getOneMember(
            array('uid' => trim($_GET['uid'])));
            $this->view()->assign("userinfo", $user);
            $this->view()->assign("uid", $_GET['uid']);
            $this->view()->assign("mx", $this->matchChargeTypes($detail));
            $this->view()->display("file:member/mx.tpl");
        } else {
            $lastMoth = strtotime("-1 month");
            $now = time();
            $this->view()->assign("time", 
            array('lastmonth' => date("Y.m.d", $lastMoth), 
            'now' => date("Y.m.d", $now)));
            $income = MemberModel::instance()->getIncomeByTime($lastMoth, $now, 
            array('status' => 8));
            $spend = MemberModel::instance()->getSpendByTime($lastMoth, $now, 
            array('status' => 8));
            $detail = MemberModel::instance()->getCouponsHistoryPage(
            $page['start'], $page['num']);
            $this->view()->assign("mx", $this->matchChargeTypes($detail));
            $this->view()->assign("coupons", 
            array('income' => $income['income'], 'spend' => $spend['spend']));
            $this->view()->display("file:member/coupons.tpl");
        }
    }
    public function import ()
    {
        $action = $_GET['action'];
        if ($action == 'import') { //导入CSV 
        //导入处理 
        } elseif ($action == 'export') { //导出CSV 
        //导出处理 
        }
        if ($action == 'import') { //导入CSV 
            $filename = $_FILES['file']['tmp_name'];
            if (empty($filename)) {
                echo '请选择要导入的CSV文件！';
                exit();
            }
            $remark = trim($_POST['remark']);
            $handle = fopen($filename, 'r');
            $result = $this->input_csv($handle); //解析csv 
            $len_result = count($result);
            if ($len_result == 0) {
                echo '没有任何数据！';
                exit();
            }
            $notMatchFile = "";
            $line = "";
            for ($i = 0; $i < $len_result; $i ++) { //循环获取各字段值 
                $name = iconv('gb2312', 'utf-8', $result[$i][0]); //中文转码 
                $coupons = $result[$i][1];
                $user = MemberModel::instance()->getOneMember(
                array('real_name' => trim($name), 'verify' => 1));
                //如果没有找到
                $format = "%s,%s,%s";
                if (empty($user)) {
                    $line = sprintf($format, $result[$i][0], $coupons, 
                    "not found");
                    $notMatchFile = $notMatchFile . $line . "\r\n";
                    continue;
                }
                $row = MemberModel::instance()->saveCoupons($user['uid'], 
                $coupons, $remark, $this->payment["收入"], $this->chargeType["充值"], 
                null, $this->status['成功']);
                //存储没有导入的数据
                if ($row < 1) {
                    $line = sprintf($format, $result[$i][0], $coupons, 
                    "not write");
                    $notMatchFile = $notMatchFile . $line . "\r\n";
                    continue;
                }
            }
            $data_values = substr($data_values, 0, - 1); //去掉最后一个逗号 
            fclose($handle); //关闭指针 
            $this->createCSV($notMatchFile);
            exit();
        }
    }
    /**
     * 如果出现错误 就需要对历史订单进行匹配了
     * 这里上次结算id截至到目前的订单
     */
    public function verify ()
    {
        $this->getOrderService()->verifyCoupons();
    }
    public function addCoupons ()
    {
      
        if (isset($_POST)) {
            $uid = $_POST['uid'];
            $coupons = trim($_POST['coupons']);
            $remark = trim($_POST['remark']);
            $row = MemberModel::instance()->saveCoupons($uid, $coupons, $remark, 
            $this->payment["收入"], $this->chargeType["充值"], null, 
            $this->status['成功']);
            if ($row > 0) {
                $this->sendNotice("充值成功");
            } else {
                $this->sendNotice("充值失败");
            }
        }
    }
    /**
     * 
     * 用户自己的订单明细
     */
    public function trade ()
    {
        if (! isset($_SESSION['uid']))
            return;
        $user = $this->getUserInfo();
        $num = 20;
        $where = array('uid' => $user['uid']);
        $totalNum = OrderModel::instance()->countOrder($where);
        $page = $this->page($totalNum['num'], $num);
        $rs = OrderModel::instance()->commentPage($where, $page['start'], 
        $page['num']);
        $this->view()->assign('totalnum', $totalNum);
        $this->view()->assign('num', $page);
        $status = array_flip($this->status);
        //匹配图片
        foreach ($rs as $k => $v) {
            $rs[$k]['status'] = $status[$v['status']];
            $rs[$k]['goodslist'] = OrderModel::instance()->getGoodsInfo(
            $v['orderno']);
            $rs[$k]['goodsnum'] = count($rs[$k]['goodslist']);
        }
        $this->view()->assign('userinfo', $user);
        $this->view()->assign("order", $rs);
        $this->view()->display("mysql:trade.tpl");
    }
    /**
     * 用户账户信息
     * */
    public function account ()
    {
        $userinfo = $this->getUserInfo();
        $this->view()->assign("userinfo", $userinfo);
        $this->view()->display("mysql:account.tpl");
    }
    /*
     * 导入
     * */
    public function csv ()
    {
        $this->view()->display("file:order/csv.tpl");
    }
    /**
     * 用户自己的账户
     * Enter description here ...
     */
    public function coupons ()
    {
        if (! isset($_SESSION[uid])) {
            return;
        }
        // 默认设置为15条
        if ($_GET['num'] > 0) {
            $num = trim($_GET['num']);
        } else {
            $newNum = require 'config/base.php';
            $num = 100;
        }
        $totalNum = 200;
        $page = $this->page($totalNum, $num);
        $user = $this->getUserInfo();
        $detail = MemberModel::instance()->getCouponsHistoryPage($page['start'], 
        $page['num'], array('a.uid' => $user['uid']));
        $this->view()->assign("mx", $this->matchChargeTypes($detail));
        $this->view()->assign('userinfo', $user);
        $this->view()->assign('totalnum', $totalNum);
        $this->view()->assign('num', $page);
        $this->view()->display("mysql:coupons.tpl");
    }
    /**
     * 打印未确认的订单
     * Enter description here ...
     */
    public function p ()
    {
        $verifyOrder = self::getOrderService()->getVerifyOrder();
        $this->setCsvHeader();
        echo "序号,订单号,收货人,地区,商品名称,单价,数量,总额,备注";
        echo "\n";
        foreach ($verifyOrder as $v) {
            //备注中不能存在,号 和换行
            $remark = str_replace(",", "", $v['remark']);
            $remark = str_replace("<br>", "", $remark);
            echo sprintf("%s,%s,%s,%s,%s,%s,%s,%s,%s", $v['id'], $v['orderno'], 
            $v['shr'], $v['address'], $v['goods_name'], $v['coupons'], $v['num'], 
            $v['coupons_total'], $remark);
            echo "\n";
        }
    }
    public function audit ()
    {
        $rs = self::getOrderService()->audit();
        //获取审计账目
        $audit = self::getOrderService()->getAudit();
        $this->view()->assign("coupons", $rs);
        $this->view()->assign('audit', $audit);
        $this->view()->display('file:order/audit.tpl');
    }
    /**
     * 获取服务累
     * Enter description here ...
     */
    public static function getOrderService ()
    {
        return new OrderService();
    }
    /**
     * 分页
     *
     * @return void
     */
    protected function page ($total, $num = 5)
    {
        $pageid = isset($_GET['p']) ? $_GET['p'] : 1;
        $rs = $pageid * $num - $total;
        $start = ($pageid - 1) * $num;
        $pagenum = ceil($total / $num);
        $page = array('start' => $start, 'num' => $num, 'current' => $pageid, 
        'page' => $pagenum);
        return $page;
    }
    private function createCSV ($content)
    {
        header("Cache-Control: public");
        header("Pragma: public");
        header("Content-type:application/vnd.ms-excel");
        $file = date("md", time());
        header("Content-Disposition:attachment;filename=$file.csv");
        echo $content;
    }
    /**
     * 获取用户信息
     * Enter description here ...
     */
    private function getUserInfo ()
    {
        $user = unserialize($_SESSION['uid']);
        return MemberModel::instance()->getOneMember(
        array('uid' => $user['uid']));
    }
    private function getOrderList ()
    {}
    /**
     * 处理收入还是支出
     * Enter description here ...
     * @param unknown_type $order
     */
    private function matchChargeTypes ($order)
    {
        $chargeTypes = array_flip($this->chargeType);
        $couponsStatus = array_flip($this->status);
        foreach ($order as $k => $v) {
            $order[$k]['chargetypes'] = $chargeTypes[$v['chargetypes']];
            $order[$k]['status'] = $couponsStatus[$v['status']];
        }
        return $order;
    }
    private function setCsvHeader ()
    {
        header("Cache-Control: public");
        header("Pragma: public");
        header("Content-type:application/vnd.ms-excel");
        $file = date("md", time());
        header("Content-Disposition:attachment;filename=$file.csv");
    }
    private function input_csv ($handle)
    {
        $out = array();
        $n = 0;
        while ($data = fgetcsv($handle, 10000)) {
            $num = count($data);
            for ($i = 0; $i < $num; $i ++) {
                $out[$n][$i] = $data[$i];
            }
            $n ++;
        }
        return $out;
    }
    /**
     * 推送服务
     * */
    private function pushOrder ($content)
    {
        $mailconfig = require 'mail.php';
        $user = require 'order.php';
        $subject = $mailconfig['subject'];
        foreach ($user as $v) {
            $subject = '你好' . $v['username'] . ' 有新的订单';
            $this->sendmail($v['email'], $subject, $content);
        }
    }
    /**
     * 发送邮件
     * Enter description here ...
     * @param unknown_type $to
     * @param unknown_type $subject
     * @param unknown_type $content
     */
    private function sendmail ($to, $subject, $content)
    {
        include_once "phpmailer/class.phpmailer.php";
        $mail = new PHPMailer();
        // 以下设置 - 不要更改!
        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->IsHTML(true);
        $mail->CharSet = "UTF-8";
        $mail->Encoding = "base64";
        // 以下设置 - 不要更改!
        $mail->AddAddress($to, "");
        $mail->Subject = $subject;
        $mail->Body = $content;
        // 以下 5 个变量值必须据实修改
        $config = require 'mail.php';
        $host = $config['host'];
        $username = $config['username'];
        $password = $config['password'];
        $from = $config['from'];
        $fromname = $config['fromname'];
        //$mail->AddReplyTo("", "");
        //$mail->AddAttachment("ok.jpg"); //附件
        // 以下设置 - 不要更改!
        $mail->Host = $host;
        $mail->Username = $username;
        $mail->Password = $password;
        $mail->From = $from;
        $mail->FromName = $fromname;
        // 发送并返回相应信息
        if (! $mail->Send()) {
            return 0;
            exit();
        }
        return 1;
    }
    /**
     * 获取产品信息
     * @return 订单名称 总价  产品件数  产品列表
     */
    private function getGoodsInfo ($data, $sno)
    {
        $moneyTotal = 0;
        $couponsTotal = 0;
        $goodsArr = array();
        $num = 0;
        $title = "";
        foreach ($data as $k => $v) {
            $rs = $this->getGoodsPrices($v['id']);
            if ($num == 0) {
                $title = $rs['title'];
            }
            $goods = array('orderno' => $sno, 'goods_id' => $v['id'], 
            'num' => $v['num'], 'goods_name' => $rs['title'], 
            'prices' => $rs['prices'], 'coupons' => $rs['coupons'], 
            'money' => $rs['prices'] * $v['num'], 
            'coupons_total' => $rs['coupons'] * $v['num']);
            $moneyTotal += $goods['money'];
            $couponsTotal += $goods['coupons_total'];
            $goodsArr[] = $goods;
            $num += $v['num'];
        }
        return array('name' => $title, 'moneytotal' => $moneyTotal, 
        'couponstotal' => $couponsTotal, 'goodslist' => $goodsArr, 'num' => $num);
    }
    /**
     * 
     * 扣除用户积分
     * @param unknown_type $coupons
     * @param unknown_type $uid
     * @param unknown_type $remark
     */
    private function saveUserCoupons ($uid, $coupons, $remark, $payment, 
    $chargetypes, $orderno, $status)
    {
        MemberModel::instance()->saveCoupons($uid, - $coupons, $remark, 
        $payment, $chargetypes, $orderno, $status);
    }
    /**
     * 扣除用户金币
     * Enter description here ...
     * @param unknown_type $money
     * @param unknown_type $uid
     * @param unknown_type $remark
     */
    private function saveUserMoney ($uid, $money, $remark, $payment, 
    $chargetypes, $orderno, $status)
    {
        MemberModel::instance()->saveMoney($uid, - $money, $remark, $payment, 
        $chargetypes, $orderno, $status);
    }
    /**
     * 
     * 添加订单
     * @param unknown_type $sno
     * @param unknown_type $name 订单名称
     * @param unknown_type $uid
     * @param unknown_type $realname
     * @param unknown_type $shr
     * @param unknown_type $address
     * @param unknown_type $mobile
     * @param unknown_type $num
     * @param unknown_type $coupons
     * @param unknown_type $money
     * @param unknown_type $remark
     */
    private function addOrder ($sno, $name, $uid, $realname, $shr, $address, 
    $mobile, $num, $coupons, $money, $remark, $status)
    {
        $params = array('uid' => $uid, 'username' => $realname, 'name' => $name, 
        'orderno' => $sno, 'shr' => $shr, 'address' => $address, 
        'mobile' => $mobile, 'addtime' => time(), 'remark' => $remark, 
        'num' => $num, 'money' => $money, 'coupons' => $coupons, 
        'status' => $status);
        $row = OrderModel::instance()->addOrder($params);
        if ($row < 1) {
            //订单添加失败
            return;
        }
    }
    /**
     * 增加订单明细  蒋getGoodsInfo中的goodsList 传递进来即可
     * @param unknown_type $goodsList
     */
    private function addGoods ($goodsList)
    {
        foreach ($goodsList as $v) {
            OrderModel::instance()->addGoods($v);
        }
        return true;
    }
    private function object_array ($array)
    {
        if (is_object($array)) {
            $array = (array) $array;
        }
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = $this->object_array($value);
            }
        }
        return $array;
    }
    /**
     * 获取产品扩展属性  产品价格和积分 代理价格 产品件数 销售
     *
     */
    private function getGoodsPrices ($gid)
    {
        $key = "IntegralValue";
        //获取价格字段
        $rs = NewsModel::instance()->getNewsWhere(array('id' => $gid), 
        'id,title');
        if (empty($rs))
            return;
        $coupons = NewsModel::instance()->getExtendVarcharByWhere($gid, 1, $key);
        if (! empty($coupons)) {
            $rs['coupons'] = $coupons[0]['value'];
        } else {
            $rs['coupons'] = 100000;
        }
        return $rs;
    }
}